package ua.org.nuos.sdms.middle.entity;

import javax.persistence.*;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: dio
 * Date: 8/24/12
 * Time: 3:25 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "user_to_group")
@NamedQueries(value = {
        @NamedQuery(name = UserToGroup.QUERY_FIND_BY_USER_ID_AND_GROUP_ID, query = "SELECT DISTINCT utg FROM UserToGroup utg WHERE utg.user.id = :userId AND utg.group.id = :groupId"),
        @NamedQuery(name = UserToGroup.QUERY_FIND_BY_USER_ID, query = "SELECT DISTINCT utg FROM UserToGroup utg WHERE utg.user.id = :userId"),
        @NamedQuery(name = UserToGroup.QUERY_FIND_BY_GROUP_ID, query = "SELECT DISTINCT utg FROM UserToGroup utg WHERE utg.group.id = :groupId"),
        @NamedQuery(name = UserToGroup.QUERY_FIND_BY_GROUP_ID_AND_STATUS, query = "SELECT DISTINCT utg FROM UserToGroup utg WHERE utg.group.id = :groupId AND utg.status = :status"),
})
public class UserToGroup implements Serializable {

    public static final String QUERY_FIND_BY_USER_ID_AND_GROUP_ID = "findUserToGroupByUserIdAndGroupId";
    public static final String QUERY_FIND_BY_USER_ID = "findUserToGroupByUserId";
    public static final String QUERY_FIND_BY_GROUP_ID = "findUserToGroupByGroupId";
    public static final String QUERY_FIND_BY_GROUP_ID_AND_STATUS = "findUserToGroupByGroupIdAndStatus";

    public static enum Status {
        LEADER, TEACHER, USER
    }

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id")
    private Group group;

    @Column(name = "status")
    @Enumerated(value = EnumType.STRING)
    private Status status;

    public UserToGroup() {
    }

    public UserToGroup(User user, Group group, Status status) {
        this.user = user;
        this.group = group;
        this.status = status;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserToGroup that = (UserToGroup) o;

        if (group != null ? !group.equals(that.group) : that.group != null) return false;
        if (status != that.status) return false;
        if (user != null ? !user.equals(that.user) : that.user != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = user != null ? user.hashCode() : 0;
        result = 31 * result + (group != null ? group.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "UserToGroup{" +
                "user=" + user +
                ", group=" + group +
                ", status=" + status +
                '}';
    }
}
